package com.interview.service.impl;

import com.interview.dao.ArticleMapper;
import com.interview.dao.CatalogueMapper;
import com.interview.dao.TitleMapper;
import com.interview.pojo.Article;
import com.interview.pojo.Catalogue;
import com.interview.pojo.Title;
import com.interview.service.CatalogueService;
import com.interview.service.TitleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service("CatalogueServiceImpl")
public class CatalogueServiceImpl implements CatalogueService {

    @Autowired
    private CatalogueMapper catalogueMapper;

    @Autowired
    private TitleMapper titleMapper;

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private TitleService titleService;

    @Override
    public void add(Catalogue catalogue) {
        try {
            System.out.println(catalogue.getTitle_id());
            int i = catalogueMapper.count(catalogue.getTitle_id());



            catalogueMapper.insertSelective(catalogue);

            //目录数量+1
            Title  title=new Title();
            title.setId(catalogue.getTitle_id());
            title.setSize(i+1);
            titleMapper.updateByPrimaryKeySelective(title);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void del(Catalogue catalogue) {
        try {
            Catalogue catalogue1 = queryID(catalogue.getId());
            System.out.println(catalogue1 );
            int i = catalogueMapper.count(catalogue1.getTitle_id());


            catalogueMapper.deleteByPrimaryKey(catalogue.getId());


            //删除此目录下的所有文章
            Article article=new Article();
            article.setCatalogue_id(catalogue1.getId());
            articleMapper.delete(article);


            //目录数量+1
            Title  title=new Title();
            title.setId(catalogue1.getTitle_id());
            title.setSize(i-1);
            titleMapper.updateByPrimaryKeySelective(title);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void up(Catalogue catalogue) {
        catalogueMapper.updateByPrimaryKeySelective(catalogue);



        //更新所有分类下的标签数量
        List<Title> titles = titleMapper.selectAll();
        for (Title title : titles) {
            String id = title.getId();
            //查询指定分类下标签数量
            int i = catalogueMapper.count(id);
            Title  title1=new Title();
            title1.setId(id );
            title1.setSize(i);
            titleMapper.updateByPrimaryKeySelective(title1);

        }


    }


    public Example createExampleSelect(Catalogue record) {
       Example example = new Example(Catalogue.class);
        Example.Criteria criteria = example.createCriteria();
        if(record!=null){
            // 如果name不为空 模糊匹配查询name字段
            if(!StringUtils.isEmpty(record.getTitle_id())){
                criteria.andEqualTo("title_id",record.getTitle_id());
            }

            //去重复
            example.setDistinct(true);
        }

        return example;
    }
    //查询 指定 方向  下的所有目录
    @Override
    public List<Catalogue> query(Catalogue catalogue) {
      List<Catalogue> select = catalogueMapper.selectByExample(createExampleSelect(catalogue));
            return  select;
    }

    //  查询全部数据
    @Override
    public List<Catalogue> queryAll() {
      List<Catalogue> catalogues = catalogueMapper.selectAll();
      return  catalogues;
    }




    @Override
    public void count() {

    }

    @Override
    public int catalogue_count_all() {
        return  catalogueMapper.catalogue_count_all();
    }

    @Override
    public Catalogue queryID(String catalogueId) {
        Catalogue catalogue1 = catalogueMapper.selectByPrimaryKey(catalogueId);
        return catalogue1;
    }
}
